package instructions;

import core.Memory;
import core.Process;
import operands.Modifier;
import operands.Operand;


public abstract class Instruction 
{
	protected Memory memory;
	protected Modifier m;
	protected Operand a;
	protected Operand b;
	
	public Instruction(Memory memory, Modifier m, Operand a, Operand b)
	{
		this.memory = memory;
		this.m = m;
		this.a = a;
		this.b = b;
		a.setInstruction(this);
		b.setInstruction(this);
	}
	
	public final void execute(Process process)
	{
		a.pre();
		b.pre();
		executeInstruction(process);
		a.post();
		b.post();
	}
	
	protected abstract void executeInstruction(Process process);

	public void setOperandA(Operand a)
	{
		this.a = a;
		a.setInstruction(this);
	}
	
	public void setOperandB(Operand b)
	{
		this.b = b;
		b.setInstruction(this);
	}
	
	public Operand getOperandA() 
	{
		return a;
	}

	public Operand getOperandB()
	{
		return b;
	}
	
	public int getAddres() 
	{
		return memory.getAddres(this);
	}

	public Memory getMemory() 
	{
		return memory;
	}
	
	public String toString()
	{
		String result = new String();
		result += m.toString() + " " + a.toString() + ", " + b.toString();
		return result;
	}

	public abstract Instruction copy();
}
